#include "cc1111rf.h"
#include "global.h"
#include "blackhat-game2.h"


/*************************************************************************************************
 * welcome to the cc1111usb application.
 * this lib was designed to be the basis for your usb-app on the cc1111 radio.  hack fun!
 *
 * 
 * best way to start is to look over the library and get a little familiar with it.
 * next, put code as follows:
 * * any initialization code that should happen at power up goes in appMainInit()
 * * the main application loop code should go in appMainLoop()
 * * usb interface code should go into appHandleEP5.  this includes a switch statement for any 
 *      verbs you want to create between the client on this firmware.
 *
 * if you should need to change anything about the USB descriptors, do your homework!  particularly
 * keep in mind, if you change the IN or OUT max packetsize, you *must* change it in the 
 * EPx_MAX_PACKET_SIZE define, the desciptor definition (be sure to get the right one!) and should 
 * correspond to the setting of MAXI and MAXO.
 * 
 * */


#define APP_NIC 0x42
#define NIC_RECV 0x1
#define NIC_XMIT 0x2
#define SNIFF_RECV 0x1

/*************************************************************************************************
 * Application Code - these first few functions are what should get overwritten for your app     *
 ************************************************************************************************/

xdata u32 recvCnt;
xdata u32 counter;
#define COUNTER_RESET 3000
TransData td;

/* build response message and return length */
u8 build_resp_mess(struct trans_data *in_td, u8 pbyte){
    
    switch (pbyte){
        case atmess:
            return ATLAS;
            break;
#ifdef LARGE_GAME
        case bhmess:
            td.type = sendbyte;
            return TYBH;
            break;
        case cutmess:
            td.type = sendbyte;
            return CUT;
            break;
        case qmess:
            td.type = sendbyte;
            return LINK;
            break;
        case tmess:
            td.type = sendbyte;
            return GOOD;
            break;
#endif  // LARGE_GAME
        default:
            return ERROR;
    }
}

/* appMainInit() is called *before Interrupts are enabled* for various initialization things. */
void appMainInit(void)
{
    recvCnt = 0;
    counter = COUNTER_RESET;

    RxMode();
    //startRX();
}

/* appMain is the application.  it is called every loop through main, as does the USB handler code.
 * do not block if you want USB to work.                                                           */


void appMainLoop(void)
{
    xdata u8 processbuffer;
    u8 pseq = 0;                // Packet Sequence 0 = init, 1 = security, 2 = data, after that we are done = 0xFF
    u8 print_byte;              // Message to print byte designator
    
    // We transmitt init, seccode, and data. Then we are done.
    if (pseq != 3){
        // Send next packet in the sequence
        build_packet(*td,pseq,0);
    }else{
        // Start over
        build_packet(*td,donebyte,0);
        pseq = 0;
    }

    // Send packet. The length is zero because we are in variable mode. First byte of data is length
    //transmit((xdata u8*)"BLACK HAT RULEZ!\n", 18);
    transmit(*td, 0);

    recvCnt++;

    if (rfif)
    {
        lastCode[0] = 0xd;
        //IEN2 &= ~IEN2_RFIE;
        
        if(rfif & RFIF_IRQ_DONE)
        {
            processbuffer = !rfRxCurrentBuffer;
            if(rfRxProcessed[processbuffer] == RX_UNPROCESSED)
            {   // we've received a packet.  deliver it.
                LED_RED = !LED_RED;
                ++recvCnt;

                /* Set receive buffer to processed so it can be used again */
                rfRxProcessed[processbuffer] = RX_PROCESSED;
            }
        }

        rfif = 0;
        //IEN2 |= IEN2_RFIE;
    }
}

/*************************************************************************************************
 * main startup code                                                                             *
 *************************************************************************************************/
void initBoard(void)
{
    clock_init();
    io_init();
}


void main (void)
{
start:
    initBoard();

    blink(300,300);

    init_RF();
    appMainInit();

    usb_up();

    /* Enable interrupts */
    EA = 1;

    while (1)
    {  

        //  LED_RED = !LED_RED;
        // timed transmit
        if (--counter == 0) {
            counter = COUNTER_RESET;
            appMainLoop();
        }
    }

}

